<template>
  <div class="container-fluid">
    <div class="row school-model school-tiao">
      <div class="col school-tiao-left">
        <span>{{ $route.name }}</span>
      </div>
      <div class="col text-right">
        <a-button-group>
          <a-button :disable="isaddress" type="primary">
            <router-link class="text-white" :to="{name: '创建通知公告'}">添加</router-link>
          </a-button>
        </a-button-group>
      </div>
    </div>
    <div class="row school-model index-model">
      <div class="col school-col-table">
        <el-table
          :data="tableData.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
          style="width: 100%; border: 1px solid #eeeeee"
        >
          <el-table-column prop="ID" label="序号" width="60">
            <template slot-scope="scope">{{scope.$index+1}}</template>>
          </el-table-column>
          <el-table-column prop="title" label="通知公告标题">
            <template slot-scope="scope">
              <router-link
                :to="{name: '通知公告详情', params: { ID: scope.row.ID }}"
                v-text="scope.row.title"
              ></router-link>
            </template>
          </el-table-column>
          <el-table-column prop="sex" label="接收率">
            <template
              slot-scope="scope"
            >{{scope.row.schoolannouncementpushstudent.filter(v=> v.type=='已查看').length}}/{{ scope.row.schoolannouncementpushstudent.length }}</template>
          </el-table-column>
          <el-table-column prop="birthday" label="发送人">
            <template slot-scope="scope">{{ getschooluser.find(v=>v.ID == scope.row.uid).name}}</template>
          </el-table-column>
          <el-table-column sortable prop="phonenum" label="发送时间">
            <template slot-scope="scope">{{ totime(scope.row.starttime)}}</template>
          </el-table-column>
          <el-table-column sortable prop="type" label="状态"></el-table-column>
          <el-table-column align="left" width="250px">
            <template slot="header" slot-scope="scope">
              <input
                class="form-control form-control-sm"
                :key="scope.$index"
                v-model.lazy="search"
                placeholder="输入关键字搜索"
              />
            </template>
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                v-show="scope.row.type !== '已发送' && getuser.ID ==scope.row.uid"
                @click="add(scope.row)"
              >发送</el-button>
              <el-button
                size="mini"
                type="warning"
                v-show="scope.row.type == '已发送' && getuser.ID ==scope.row.uid"
                @click="state(scope.row)"
              >撤回</el-button>
              <el-button
                size="mini"
                v-show="getuser.ID ==scope.row.uid"
                type="danger"
                @click="del(scope.row.ID)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          @current-change="currentChange"
          :page-size="pagesize"
          layout="total, prev, pager, next"
          :total="tableData.length"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  asynccreatschoolannouncementpush,
  asyncdelschoolannouncementpush
} from "../../../../plugins/api.js";
import { totime } from "../../../utils.js";
export default {
  name: "announcement",
  data() {
    return {
      search: "",
      currentPage: 1,
      pagesize: 10,
      isaddress: true
    };
  },
  computed: {
    ...mapGetters([
      "getschoolstudent",
      "getschooluser",
      "getschoolannouncementpush",
      "getuser"
    ]),
    tableData() {
      const { getschoolannouncementpush, search } = this;
      return getschoolannouncementpush.filter(data => {
        return Object.values(data)
          .join("")
          .toLowerCase()
          .includes(search.toLowerCase());
      });
    }
  },
  mounted() {
    if (this.getschoolstudent.length) {
      this.isaddress = false;
    }
  },
  methods: {
    totime,
    currentChange(index) {
      this.currentPage = index;
    },
    del(ID) {
      this.$confirm("此操作将永久删除该公告, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "danger"
      })
        .then(async () => {
          try {
            await asyncdelschoolannouncementpush({ ID });
            await this.$store.dispatch("actionschoolannouncementpush");

            this.$message.success("删除成功");
          } catch (err) {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async state(row) {
      this.$confirm(`是否撤回${row.title}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            let r = JSON.parse(JSON.stringify(row));
            r.type = "已撤回";
            await asynccreatschoolannouncementpush(r);
            await this.$store.dispatch("actionschoolannouncementpush");
            this.$message.success("撤回成功");
          } catch (err) {
            this.$message.error(`撤回失败：${err}`);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    async add(row) {
      this.$confirm(`是否发送${row.title}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            let r = JSON.parse(JSON.stringify(row));
            r.type = "已发送";
            await asynccreatschoolannouncementpush(r);
            await this.$store.dispatch("actionschoolannouncementpush");
            this.$message.success("发送成功");
          } catch (err) {
            this.$message.error(`发送失败：${err}`);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.index-model {
  margin-top: 13px;
}
</style>